// miniprogram/pages/mycard/mycard.js
//先获取到App
const app = getApp()

wx.cloud.init();
const db = wx.cloud.database()

Page({
    data: {
        words:[],
        selected:0,
        firstPrize:true,
        prizeArr:[
            '../../images/avPrice.png',
            '../../images/mycard_icon_onepreice.png',
            '../../images/towPrice.png',
            '../../images/三等奖.png',
        ],
        prize:0
    },

    onLoad: function (options) {
        db.collection('test02').where({
            "_openid": app.globalData.openid
        }).get().then((res) => { // get 方法会触发网络请求，往数据库取数据
        console.log('myCard',res);
            if(res.data.length > 0 ){
                let words = res.data[0].words;
                this.setData({
                    words:words,
                })
            }
        })
    },

    select(e){
        //下标
        let index = e.currentTarget.dataset.index;
        //获取data中的words
        let { words,selected } = this.data;

        //限制用户必须拥有这个字  number !=0
        if(words[index].number == 0){
            return ;
        }

        if( selected>=2 && !words[index].check ){
            return;
        }
        if(!words[index].check){
            //未选中
            words[index].check = true;
            selected++;
        }else{
            //已选中
            words[index].check = false;
            selected--;
        }

        this.setData({
            words:words,
            selected:selected
        })
    },

    // 合成按钮
    merge(){
        //selected必须为2
        let { selected , words } = this.data;
        if(selected <2){
            wx.showToast({
                title: '必须选中两个邮卡',
                icon:'none',
                image:'',
                duration:1500,
            });
            return;
        }

        let selectedArr = [];
        words.forEach((item,index)=>{
            if(item.check == true){
                selectedArr.push(index);
            }
        });
        let firstPrize=true;
        wx.cloud.callFunction({
            name:'merge',
            data:{
                selectedArr:selectedArr
            },
            success:(res)=>{
                console.log('merge成功调用',res);
                if(res.result.success){
                    let prize = res.result.prize;
                    let firstPrize = false;
                    this.setData({
                        firstPrize: firstPrize,
                        prize:prize
                    })
                    wx.showToast({
                        title:'你抽到了'+prize+'等奖',
                        icon:'sucess',
                        duration:1500,
                    });

                    this.setData({
                        words:res.result.words
                    })
                }else{
                    wx.showToast({
                        title:res.result.msg,
                        icon:'none',
                        duration:1500,
                    });
                }
            },
            fail:(err)=>{
                console.log(err);
            }
        })
    },

    // 点击填写资料关闭弹窗
    write_btn(){
        wx.redirectTo({
            url: `../write/write?prize=${this.data.prize}`,
        });
        this.setData({
            firstPrize: true
        });
    },

   

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})